<template>
    <el-form
        ref="ruleFormRef"
        style="width: 250px; padding: 20px"
        :model="ruleForm"
        status-icon
        :rules="rules"
        label-width="auto"
        hide-required-asterisk
        @keyup.enter="submitForm(ruleFormRef)"
    >
        <div
            style="
                display: flex;
                justify-content: center;
                align-items: center;
                margin-bottom: 20px;
                font-size: 16px;
            "
        >
            <img
                src="../../assets/logo/icon128.png"
                style="width: 20px; height: 20px; margin-right: 10px"
                alt=""
            />
            <div>插件登录</div>
        </div>
        <el-form-item label="账号" prop="username">
            <el-input
                v-model="ruleForm.username"
                type="text"
                :prefix-icon="User"
            />
        </el-form-item>
        <el-form-item label="密码" prop="password">
            <el-input
                v-model="ruleForm.password"
                type="password"
                show-password
                :prefix-icon="Lock"
            />
        </el-form-item>
        <el-form-item>
            <el-button
                type="primary"
                style="width: 100%"
                @click="submitForm(ruleFormRef)"
            >
                提交
            </el-button>
        </el-form-item>
    </el-form>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
// @ts-ignore
import { User, Lock } from '@element-plus/icons-vue'
import { login } from '../../api/demo'
import { loginFlagStore } from '../../store'
import { setToken } from '../../utils/auth'
const ruleFormRef = ref<FormInstance>()

const store = loginFlagStore()

const ruleForm = reactive({
    username: '',
    password: '',
})

const rules = reactive<FormRules<typeof ruleForm>>({
    username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
    password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
})

const submitForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.validate(async (valid) => {
        if (valid) {
            const result: any = await login({
                username: ruleForm.username,
                password: ruleForm.password,
            })
            console.log('res1', result)
            const data = await setToken(result.token)
            store.setLoginFlag(true)
            console.log('res2', data)
            // 判断是不是打开的popup.html，如果是的话，则刷新当前被激活的tabs页
            if (!chrome.tabs) return
            chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
                const activeTab = tabs[0]
                if (activeTab && activeTab.id !== undefined) {
                    chrome.tabs.reload(activeTab.id)
                }
            })
        } else {
            console.log('error submit!')
        }
    })
}
</script>
